﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crude.Common;
using System.IO;
using System.Runtime.Serialization;

namespace Crude.Core
{
    public class SupportedOutputTypeLoader
    {
        const string OutTypeFilename = "OutputTypes.xml";
        
        public IDictionary<string, SupportedOutputType> Load()
        {
            string filename = string.Format("{0}\\{1}", new CrudeSettingsStateLoader().Load().CrudeBinPath, OutTypeFilename);

            using (FileStream fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read))
            {
                DataContractSerializer dataContractSerializer = new DataContractSerializer(typeof(IList<SupportedOutputType>));
                return  ((IList<SupportedOutputType>)dataContractSerializer.ReadObject(fileStream)).
                    ToDictionary( s => s.FileType, s => s);                
            }        
        }

        public void Temp()
        {
            string filename = string.Format("{0}\\{1}", new CrudeSettingsStateLoader().Load().CrudeBinPath, OutTypeFilename);

            using (FileStream fileStream = new FileStream(filename, FileMode.Create))
            {
                DataContractSerializer dataContractSerializer = new DataContractSerializer(typeof(IList<SupportedOutputType>));

                IList<SupportedOutputType> s = new List<SupportedOutputType>
                {
                    new SupportedOutputType()
                    {
                        Extension = "1"
                    }
                };

                dataContractSerializer.WriteObject(fileStream, s);

                fileStream.Flush();
            }   
        }
    }
}
